package org.BUPT.application.service;

import org.BUPT.domain.model.question.QuestionBO;
import org.BUPT.domain.service.impl.QuestionServiceImpl;
import org.BUPT.infrastructure.exception.NullObjectException;
import org.BUPT.infrastructure.exception.PersistException;
import org.BUPT.infrastructure.persistent.po.Question;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class QuestionService {
    @Autowired
    org.BUPT.domain.service.QuestionService questionService;


    public QuestionBO getQuestion(int id) throws NullObjectException, PersistException {
        Question question = questionService.getQuestion(id);
        if (question!=null&&question.getIsDel()!=1){
            return new QuestionBO(question);
        }
        else{
            throw new NullObjectException("所查询的问题不存在");
        }
    }

    public QuestionBO createQuestion(String title,int userid,String content,int subjectId,String[] tags) throws PersistException {
        return new QuestionBO()
                .setTitle(title)
                .setContent(content)
                .setUserId(userid)
                .setCreateTime(new Date())
                .setTags(Arrays.asList(tags))
                .setSubjectId(subjectId);
    }

    public List<QuestionBO> getQuestionPage(int pageId) throws NullObjectException, PersistException {
        List<Question> questions = questionService.getPage(pageId);
        List<QuestionBO> questionBOS = new ArrayList<>();
        for (Question question : questions) {
            QuestionBO questionBO = new QuestionBO(question).view();
            questionBOS.add(questionBO);
        }
        return questionBOS;
    }
    //    public List<String> getAllSubjects(){
//        return questionRepository.getAllSubjects();
//    }
    public List<QuestionBO> getSubscribedQuestion(int userId) throws NullObjectException, PersistException {
        List<Question> questions = questionService.getSubscribedQuestions(userId);
        List<QuestionBO> questionBOS = new ArrayList<>();
        for (Question question:questions){

            QuestionBO questionBO = new QuestionBO(question).view();
            questionBOS.add(questionBO);
        }
        return questionBOS;
    }
    public List<QuestionBO> getAskedQuestion(int userId) throws NullObjectException, PersistException {
        List<Question> questions = questionService.getAskedQuestions(userId);
        List<QuestionBO> questionBOS = new ArrayList<>();
        for (Question question:questions){
            QuestionBO questionBO = new QuestionBO(question).view();
            questionBOS.add(questionBO);
        }
        return questionBOS;
    }

    public void subscribeQuestion(int userId,int questionId){
        questionService.subscribeQuestion(userId,questionId);
    }

    public void unsubscribeQuestion(int userId, int questionId) {
        questionService.unSubscribe(userId,questionId);
    }

}
